//公共校验
//手机号校验
import { ElMessage } from "element-plus";
import type {goodsStats} from "@/types/index"
export const phoneCheck = (val:string) =>{
    if (!/^1\d{10}$/.test(val)) {
    throw ElMessage({ message: "请输入正确的手机号", duration: 1500 });
  }
}
//密码校验
export const passwordCheck = (val:string) =>{
    if (!/^(?=.*\d)(?=.*[a-zA-Z]).{6,8}$/.test(val.trim())) {
    throw ElMessage({ message: "密码需要6-8位数字和字母组合", duration: 1500 });
  }
}

export const nullCheck = (val: string, tips: string) => {
  if (val.trim() === "") {
    throw ElMessage({ message: tips, duration: 1500 });
  }
};
// 商品上传的规格校验
export const validateSpecs = (val: goodsStats[], specstype: number) => {
  if (specstype === 1) {
    // 无规格
    for (const item of val) {
      if (item.productPrice === "" || item.productStock === "") {
        throw ElMessage({ message: "请填写售价或库存", duration: 1500 });
      }
    }
  } else {
    // 有规格
    for (const item of val) {
      if (item.productPrice === "" || item.productStock === "" || item.productAttributes === "") {
        throw ElMessage({ message: "请完善规格", duration: 1500 });
      }
    }
  }
};
